﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrm.Services.Realization;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Maticsoft.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using System.Linq.Dynamic.Core;
using Org.BouncyCastle.Ocsp;
using Microsoft.EntityFrameworkCore.Internal;
using NPOI.SS.Formula.Eval;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.Extensions.Logging;

namespace FangChanCrm.Controllers
{

    public class PhoneKeHuController : Controller
    {
        private readonly It_KeHuBiaoService it_KeHuBiaoService;
        private readonly It_XiaoShouBiaoService it_XiaoShouBiaoService;
        private readonly It_XiaoShouXiangXiBiaoService it_XiaoShouXiangXiBiaoService;
        private readonly It_RenYuanBiaoService it_RenYuanBiaoService;
        private readonly It_XiaoXiTiXingBiaoService it_XiaoXiTiXingBiaoService;
        private readonly It_KeHuZiYuanChiService it_KeHuZiYuanChiService;
        private readonly It_GangWeiBiaoService it_GangWeiBiaoService;
        private readonly It_AnChangBiaoService it_AnChangBiaoService;
        private readonly It_FangYuanBiaoService it_FangYuanBiaoService;

        public PhoneKeHuController(It_KeHuBiaoService it_KeHuBiaoService, It_XiaoShouBiaoService it_XiaoShouBiaoService, It_XiaoShouXiangXiBiaoService it_XiaoShouXiangXiBiaoService, It_RenYuanBiaoService it_RenYuanBiaoService, It_XiaoXiTiXingBiaoService it_XiaoXiTiXingBiaoService, It_KeHuZiYuanChiService it_KeHuZiYuanChiService, It_GangWeiBiaoService it_GangWeiBiaoService, It_AnChangBiaoService it_AnChangBiaoService, IConfiguration configuration, It_FangYuanBiaoService it_FangYuanBiaoService)
        {
            this.it_KeHuBiaoService = it_KeHuBiaoService;
            this.it_XiaoShouBiaoService = it_XiaoShouBiaoService;
            this.it_XiaoShouXiangXiBiaoService = it_XiaoShouXiangXiBiaoService;
            this.it_RenYuanBiaoService = it_RenYuanBiaoService;
            this.it_XiaoXiTiXingBiaoService = it_XiaoXiTiXingBiaoService;
            this.it_KeHuZiYuanChiService = it_KeHuZiYuanChiService;
            this.it_GangWeiBiaoService = it_GangWeiBiaoService;
            this.it_AnChangBiaoService = it_AnChangBiaoService;
            Configuration = configuration;
            this.it_FangYuanBiaoService = it_FangYuanBiaoService;
        }

        public IConfiguration Configuration { get; }






        // GET: /<controller>/


        public IActionResult KeHuAdd()
        {
            return View();
        }
        public IActionResult KeHuUpDate()
        {
            return View();
        }
        public IActionResult WoDeKeHu()
        {
            return View();
        }
        public IActionResult KeHuGenJinJiLu()
        {
            return View();
        }
        public IActionResult BaoBei()
        {
            //if (HttpContext.Session.GetString("loginid") == null)
            //{
            //    HttpContext.Request.Cookies.TryGetValue("账户", out string value);
            //    if (value == null || value == "")
            //    {
            //        return View("../Login/Login");
            //    }
            //    else
            //    {
            //        return View("../Login/LoginChongXin");
            //    }
            //}
            //else
            //{
            //    return View();
            //}
            return View();
        }

        public IActionResult GenJin()
        {
            return View();
        }

        public IActionResult GenJinList()
        {
            //if (HttpContext.Session.GetString("loginid") == null)
            //{
            //    HttpContext.Request.Cookies.TryGetValue("账户", out string value);
            //    if (value == null || value == "")
            //    {
            //        return View("../Login/Login");
            //    }
            //    else
            //    {
            //        return View("../Login/LoginChongXin");
            //    }
            //}
            //else
            //{
            //    return View();
            //}
            return View();
        }


        public IActionResult GetKeHu()
        {
            return View();
        }

        public IActionResult GenJinJiLuToday()
        {
            return View();
        }

        public IActionResult KeHuXiangXi()
        {
            return View();
        }
        public IActionResult KeHuLeiXingList()
        {
            return View();
        }

        public IActionResult LaiFang()
        {
            return View();
        }


        public IActionResult My_Customer()
        {



            //int currentCount = 0;
            //currentCount += 1;
            //string shuzhi = currentCount.ToString().Trim();
            //timer1 = new System.Timers.Timer();
            //timer1.Interval = 7200000;  //设置计时器事件间隔执行时间  每隔两秒执行一次
            //timer1.Elapsed += new System.Timers.ElapsedEventHandler(CallWithAsyns);
            //timer1.Enabled = false;

            CallWithAsyns();//异步方法
            return View();
        }

        //异步方法 客户是否失效    object sender, System.Timers.ElapsedEventArgs e
        public async void CallWithAsyns()
        {
            //获取人员id
            int UserId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));

            await Task.Run(() =>
            {
                Task task = new Task(() =>
                {
                    Thread.Sleep(5000);


                    var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                    optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                    //存销售信息
                    List<t_XiaoShouBiao> xiaoshou = new List<t_XiaoShouBiao>();
                    //存销售详细信息
                    List<t_XiaoShouXiangXiBiao> xiaoshouxiangxi = new List<t_XiaoShouXiangXiBiao>();
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        //人员岗位
                        var gangweiid = _context.t_RenYuanBiao.FirstOrDefault(x => x.Id == UserId).GangWeiId;
                        //判断是 业务员或是置业顾问
                        if (gangweiid == 32)
                        {
                            //销售信息
                            xiaoshou = _context.t_XiaoShouBiao.Where(x => x.YeWuYuanId == UserId).ToList();
                        }
                        else if (gangweiid == 34)
                        {
                            //销售信息
                            xiaoshou = _context.t_XiaoShouBiao.Where(x => x.ZhiYeGuWenId == UserId).ToList();
                        }

                        //循环销售长度
                        for (int i = 0; i < xiaoshou.Count(); i++)
                        {
                            xiaoshou[i].t_RenYuanBiao_YeWuYuan = null;
                            xiaoshou[i].t_RenYuanBiao_ZhiYeGuWen = null;
                            //销售详细赋值
                            var xiaoshoushuju = _context.t_XiaoShouXiangXiBiao.Where(x => x.XiaoShouBiaoId == xiaoshou[i].Id).ToList();
                            //每条销售id循环销售详细长度
                            for (int j = 0; j < xiaoshoushuju.Count(); j++)
                            {
                                xiaoshoushuju[j].t_XiaoShouBiao = null;
                                //查出销售id 所有销售详细 赋值
                                xiaoshouxiangxi.Add(xiaoshoushuju[j]);
                            }


                            //客户信息
                            var kehuxinxi = _context.t_KeHuBiao.Where(x => x.Id == xiaoshou[i].KeHuId).ToList();
                            kehuxinxi[0].t_XiaoShouBiao = null;



                            //最大跟进Id
                            int xiaoshouxiangxiidgenjin = 0;
                            //判断客户是否已经是已失效
                            if (kehuxinxi[0].ZhuangTai != "已失效")
                            {
                                //用 销售id 去查 销售详细 报备信息id最大的一条
                                string caozuoshijian = xiaoshouxiangxi.Where(x => x.XiaoShouBiaoId == xiaoshou[i].Id && x.XiaoShouDongZuo == "报备").ToList()[0].CaoZuoShiJian;
                                //判断系统时间是否大于报备的十五天之后
                                if (DateTime.Now < Convert.ToDateTime(caozuoshijian).AddDays(15))
                                {
                                }
                                else
                                {
                                    //用 销售id 去查 销售详细 判断 报备最开始的操作时间 和 十五天之后的时间里 是否有 记录
                                    List<t_XiaoShouXiangXiBiao> Xiaoshouxiangxibiao = xiaoshouxiangxi.ToList().Where(x => x.XiaoShouBiaoId == xiaoshou[i].Id && Convert.ToDateTime(x.CaoZuoShiJian) > Convert.ToDateTime(caozuoshijian) && Convert.ToDateTime(x.CaoZuoShiJian) <= Convert.ToDateTime(caozuoshijian).AddDays(15)).ToList();
                                    if (Xiaoshouxiangxibiao.Count() > 0)
                                    {
                                        xiaoshouxiangxiidgenjin = xiaoshouxiangxi.Where(x => x.XiaoShouBiaoId == xiaoshou[i].Id && x.XiaoShouDongZuo != "报备").ToList().Max(x => x.Id);
                                        //用 销售详细id  去查  销售详细的操作时间
                                        string caozuoshijiangenjin = xiaoshouxiangxi.Where(x => x.Id == Convert.ToInt32(xiaoshouxiangxiidgenjin)).ToList()[0].CaoZuoShiJian;
                                        //判断系统时间是大于跟进三十天之后
                                        if (DateTime.Now < Convert.ToDateTime(caozuoshijiangenjin).AddDays(30))
                                        {
                                        }
                                        else
                                        {
                                            var KeHu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == kehuxinxi[0].Id);
                                            KeHu.ZhuangTai = "已失效";
                                            _context.SaveChanges();
                                        }

                                    }
                                    else
                                    {
                                        var KeHu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == kehuxinxi[0].Id);
                                        KeHu.ZhuangTai = "已失效";
                                        _context.SaveChanges();
                                    }
                                }
                            }


                            ////最大跟进Id
                            //int xiaoshouxiangxiidgenjin = 0;
                            //int panduan = 1;
                            //while (panduan >= 1)
                            //{
                            //    switch (panduan)
                            //    {
                            //        case 1://查询报备是否失效

                            //            //判断客户是否已经是已失效
                            //            if (kehuxinxi[0].ZhuangTai != "已失效")
                            //            {
                            //                //用 销售id 去查 销售详细 报备信息id最大的一条
                            //                string caozuoshijian = xiaoshouxiangxi.Where(x => x.XiaoShouBiaoId == xiaoshou[i].Id && x.XiaoShouDongZuo == "报备").ToList()[0].CaoZuoShiJian;
                            //                //判断系统时间是否大于报备的十五天之后
                            //                if (DateTime.Now < Convert.ToDateTime(caozuoshijian).AddDays(15))
                            //                {
                            //                    panduan = -2;
                            //                }
                            //                else
                            //                {
                            //                    //用 销售id 去查 销售详细 判断 报备最开始的操作时间 和 十五天之后的时间里 是否有 记录
                            //                    List<t_XiaoShouXiangXiBiao> Xiaoshouxiangxibiao = xiaoshouxiangxi.ToList().Where(x => x.XiaoShouBiaoId == xiaoshou[i].Id && Convert.ToDateTime(x.CaoZuoShiJian) > Convert.ToDateTime(caozuoshijian) && Convert.ToDateTime(x.CaoZuoShiJian) <= Convert.ToDateTime(caozuoshijian).AddDays(15)).ToList();
                            //                    if (Xiaoshouxiangxibiao.Count() > 0)
                            //                    {
                            //                        xiaoshouxiangxiidgenjin = xiaoshouxiangxi.Where(x => x.XiaoShouBiaoId == xiaoshou[i].Id && x.XiaoShouDongZuo != "报备").ToList().Max(x => x.Id);
                            //                        panduan = 2;
                            //                    }
                            //                    else
                            //                    {
                            //                        panduan = 3;
                            //                    }
                            //                }
                            //            }
                            //            else
                            //            {
                            //                panduan = 4;
                            //            }
                            //            break;
                            //        case 2://报备没有失效   判断跟进是否失效

                            //            //用 销售详细id  去查  销售详细的操作时间
                            //            string caozuoshijiangenjin = xiaoshouxiangxi.Where(x => x.Id == Convert.ToInt32(xiaoshouxiangxiidgenjin)).ToList()[0].CaoZuoShiJian;
                            //            //判断系统时间是大于跟进三十天之后
                            //            if (DateTime.Now < Convert.ToDateTime(caozuoshijiangenjin).AddDays(30))
                            //            {
                            //                panduan = -3;
                            //            }
                            //            else
                            //            {
                            //                panduan = 3;
                            //            }
                            //            break;
                            //        case 3://已失效修改客户信息

                            //            var KeHu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == kehuxinxi[i].Id);
                            //            KeHu.ZhuangTai = "已失效";
                            //            if (_context.SaveChanges() > 0)
                            //            {
                            //                panduan = 4;
                            //            }
                            //            else
                            //            {
                            //                panduan = -4;
                            //            }
                            //            break;
                            //        default:
                            //            break;
                            //    }
                            //}
                        }
                    }
                });
                task.Start();
                task.Wait(1000);
            });
        }

        public IActionResult MyCustomerDetail()
        {
            try
            {
                var code = Request.Query["code"];
                if (code.ToString() == "" || code.ToString() == null)
                {
                    int UserId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
                    t_RenYuanBiao renyuan = it_RenYuanBiaoService.GetById(UserId);
                    ViewData["GangWeiId"] = renyuan.GangWeiId;
                    ViewData["RenYuanId"] = renyuan.Id;
                    return View();
                }
                else
                {
                    string access_token = WeiXin_Common.GetAccess_Token();
                    string json = WeiXin_Common.GetUserInfo(code, access_token);
                    string WeiXinUserId = WeiXin_Common.GetJsonValue(json, "UserId");
                    IList<t_RenYuanBiao> list = it_RenYuanBiaoService.GetList().Where(x => x.BeiZhu1 == WeiXinUserId).ToList();
                    if (list.Count > 0)
                    {
                        HttpContext.Session.SetString("UserId", list[0].Id.ToString());
                        HttpContext.Session.SetString("loginid", list[0].Id.ToString());

                        ViewData["GangWeiId"] = list[0].GangWeiId;
                        ViewData["RenYuanId"] = list[0].Id;
                        return View();
                    }
                    else
                    {
                        ViewData["WeiXinUserId"] = WeiXinUserId;
                        return View("../RenYuan/BindWeiXin");
                    }
                }
            }
            catch (Exception)
            {
                return View("../RenYuan/BindWeiXin");
                //throw;
            }
        }

        public IActionResult QueRenDaoFang()
        {
            var code = Request.Query["code"];

            string access_token = WeiXin_Common.GetAccess_Token();
            string json = WeiXin_Common.GetUserInfo(code, access_token);
            string WeiXinUserId = WeiXin_Common.GetJsonValue(json, "UserId");
            IList<t_RenYuanBiao> list = it_RenYuanBiaoService.GetList().Where(x => x.BeiZhu1 == WeiXinUserId).ToList();
            if (list.Count > 0)
            {
                HttpContext.Session.SetString("UserId", list[0].Id.ToString());
                HttpContext.Session.SetString("loginid", list[0].Id.ToString());
                return View();
            }
            else
            {
                ViewData["WeiXinUserId"] = WeiXinUserId;
                return View("../RenYuan/BindWeiXin");
            }
        }

        public IActionResult DaiKanLv()
        {
            int gangweiid = (int)it_RenYuanBiaoService.GetById(Convert.ToInt32(HttpContext.Session.GetString("loginid"))/*3*/).GangWeiId;
            string gangwei = it_GangWeiBiaoService.GetById(gangweiid).GangWeiMingCheng;
            if (gangwei == "业务员" || gangwei == "置业顾问")
            {
                ViewData["panduan"] = "员工";
                return View();
            }
            else
            {
                ViewData["panduan"] = "领导";
                return View();
            }


        }

        [HttpPost]
        public string BindKeHuLeiXingLaiFang()
        {
            int id = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
            return JsonConvert.SerializeObject(it_KeHuBiaoService.GetList().Where(x => x.ZhuangTai == Request.Form["ZhuangTai"] && x.RenYuanId == id).ToList(), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }

        [HttpPost]
        public string AddKeHu()
        {
            //string LianXiFangShiPanDuan = Request.Form["LianXiFangShiPanDuan"];
            //string ShouJiHaoMa = Request.Form["ShouJiHaoMa"];
            //string XiaoQu = Request.Form["XiaoQu"];
            //List<t_KeHuBiao> yanzhengkehu = it_KeHuBiaoService.GetList().Where(x => x.ShouJiHaoMa == ShouJiHaoMa && x.YiXiangXinXi == XiaoQu).ToList();

            ////var fangwuxinxi = it_KeHuBiaoService.GetList().Where(x => x.ShouJiHaoMa == Request.Form["ShouJiHaoMa"] && x.YiXiangXinXi == Request.Form["XiaoQu"]).ToList();
            ////int panduanzhi = 0;
            ////for (int i = 0; i < fangwuxinxi.Count(); i++)
            ////{
            ////    if (panduanzhi < fangwuxinxi[i].Id)
            ////    {
            ////        panduanzhi = fangwuxinxi[i].Id;
            ////    }
            ////}
            ////var zhuangtaizhi = "已签约";
            ////if (panduanzhi != 0)
            ////{
            ////    zhuangtaizhi = it_KeHuBiaoService.GetById(panduanzhi).ZhuangTai;
            ////}

            ////if (zhuangtaizhi == "已签约")
            ////{
            //if (yanzhengkehu.Count > 0)
            //{
            //    return "errorShouJi";
            //}
            //else
            //{
            //    if (LianXiFangShiPanDuan != "")
            //    {
            //        int shujiu = Convert.ToInt32(LianXiFangShiPanDuan);
            //        it_KeHuZiYuanChiService.Delete(shujiu);
            //    }

            //    //获取当前登录人基本信息
            //    t_RenYuanBiao renyuan = it_RenYuanBiaoService.GetById(Convert.ToInt32(HttpContext.Session.GetString("UserId")));
            //    //获取所有职业顾问岗位
            //    List<t_GangWeiBiao> listgw = it_GangWeiBiaoService.GetList().Where(x => x.GangWeiMingCheng == "置业顾问").ToList();
            //    bool zhiye = false;
            //    try
            //    {
            //        for (int i = 0; i < listgw.Count; i++)
            //        {
            //            if (listgw[i].Id == renyuan.GangWeiId)
            //            {
            //                zhiye = true;
            //                break;
            //            }

            //        }
            //    }
            //    catch (Exception)
            //    {
            //        return "error";
            //        //throw;
            //    }
            //    string ZhuangTai = "已推荐";
            //    if (zhiye == true)
            //    {
            //        ZhuangTai = "已到访";
            //    }


            //    t_KeHuBiao t_KeHuBiao = new t_KeHuBiao
            //    {
            //        KeHuXingMing = Request.Form["XingMing"].ToString(),
            //        ShouJiHaoMa = ShouJiHaoMa,
            //        YiXiangDengJi = Request.Form["YiXiangDengJi"].ToString(),
            //        LianMingKeHu = Request.Form["LianMingKeHu"].ToString(),
            //        NianLing = Request.Form["NianLing"].ToString(),
            //        XingBie = Request.Form["XingBie"].ToString(),
            //        ZhengJianLeiXing = Request.Form["ZhengJianLeiXing"].ToString(),
            //        ZhengJianHaoMa = Request.Form["ZhengJianHaoMa"].ToString(),
            //        RenZhiMeiTi = Request.Form["RenZhiMeiTi"].ToString(),
            //        KeHuBeiZhu = Request.Form["KeHuBeiZhu"].ToString(),
            //        GeRenBiaoQian = Request.Form["GeRenBiaoQian"].ToString(),
            //        ZhiYeShenFen = Request.Form["ZhiYeShenFen"].ToString(),
            //        JiaTingJieGou = Request.Form["JiaTingJieGou"].ToString(),
            //        JiaoYuChengDu = Request.Form["JiaoYuChengDu"].ToString(),
            //        HunYinZhuangTai = Request.Form["HunYinZhuangTai"].ToString(),
            //        ShouRuQingKuang = Request.Form["ShouRuQingKuang"].ToString(),
            //        GongZuoQuYu = Request.Form["GongZuoQuYu"].ToString(),
            //        GOngZuoDiZhi = Request.Form["GOngZuoDiZhi"].ToString(),
            //        JuZhuQuYu = Request.Form["JuZhuQuYu"].ToString(),
            //        JuZhuDiZhi = Request.Form["JuZhuDiZhi"].ToString(),
            //        YiXiangXinXi = XiaoQu,
            //        YiXiangYongTu = Request.Form["YiXiangYongTu"].ToString(),
            //        YiXiangYeTai = Request.Form["YiXiangYeTai"].ToString(),
            //        YiXiangHuXing = Request.Form["YiXiangHuXing"].ToString(),
            //        YiXiangLouCeng = Request.Form["YiXiangLouCeng"].ToString(),
            //        YiXiangMianJi = Request.Form["YiXiangMianJi"].ToString(),
            //        YiXiangTaoShu = Request.Form["YiXiangTaoShu"].ToString(),
            //        YiXiangDanJia = Request.Form["YiXiangDanJia"].ToString(),
            //        YiXiangZongJia = Request.Form["YiXiangZongJia"].ToString(),
            //        GuanZhuNeiRong = Request.Form["GuanZhuNeiRong"].ToString(),
            //        ZhuangTai = ZhuangTai,
            //        RenYuanId = Convert.ToInt32(HttpContext.Session.GetString("UserId")),
            //        //RenYuanId = 2,
            //        BeiZhu1 = System.DateTime.Now.ToString()
            //    };
            //    int KeHuId = it_KeHuBiaoService.Add(t_KeHuBiao).Id;
            //    if (KeHuId > 0)
            //    {

            //        t_XiaoShouBiao xiaoshoubiao = new t_XiaoShouBiao();
            //        xiaoshoubiao.KeHuId = KeHuId;
            //        xiaoshoubiao.ShengChengShiJian = System.DateTime.Now.ToString();
            //        //xiaoshoubiao.YeWuYuanId = 2;
            //        if (zhiye == true)
            //        {
            //            xiaoshoubiao.ZhiYeGuWenId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            //        }
            //        else
            //        {
            //            xiaoshoubiao.YeWuYuanId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            //        }

            //        int XiaoShouBiaoId = it_XiaoShouBiaoService.Add(xiaoshoubiao).Id;

            //        t_XiaoShouXiangXiBiao xiaoshouxiangxi = new t_XiaoShouXiangXiBiao();
            //        xiaoshouxiangxi.XiaoShouDongZuo = "报备";
            //        xiaoshouxiangxi.CaoZuoShiJian = System.DateTime.Now.ToString();
            //        //xiaoshouxiangxi.CaoZuoRen = 2;
            //        xiaoshouxiangxi.CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            //        xiaoshouxiangxi.XiaoShouBiaoId = XiaoShouBiaoId;
            //        xiaoshouxiangxi.MiaoShu = "新增-报备";
            //        xiaoshouxiangxi.ZhuangTai = "";
            //        it_XiaoShouXiangXiBiaoService.Add(xiaoshouxiangxi);


            //        if (ZhuangTai == "已到访")
            //        {
            //            //t_XiaoShouBiao xiaoshoubiao1 = new t_XiaoShouBiao();
            //            //xiaoshoubiao1.KeHuId = KeHuId;
            //            //xiaoshoubiao1.ShengChengShiJian = System.DateTime.Now.ToString();
            //            ////xiaoshoubiao.YeWuYuanId = 2;
            //            //if (zhiye == true)
            //            //{
            //            //    xiaoshoubiao.ZhiYeGuWenId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            //            //}
            //            //else
            //            //{
            //            //    xiaoshoubiao.YeWuYuanId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            //            //}
            //            //int XiaoShouBiaoId1 = it_XiaoShouBiaoService.Add(xiaoshoubiao1).Id;

            //            t_XiaoShouXiangXiBiao xiaoshouxiangxi1 = new t_XiaoShouXiangXiBiao();
            //            xiaoshouxiangxi1.XiaoShouDongZuo = "确认到访";
            //            xiaoshouxiangxi1.CaoZuoShiJian = System.DateTime.Now.ToString();
            //            //xiaoshouxiangxi.CaoZuoRen = 2;
            //            xiaoshouxiangxi1.CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            //            xiaoshouxiangxi1.XiaoShouBiaoId = XiaoShouBiaoId;
            //            xiaoshouxiangxi1.MiaoShu = "新增-确认到访";
            //            xiaoshouxiangxi1.ZhuangTai = "";
            //            it_XiaoShouXiangXiBiaoService.Add(xiaoshouxiangxi1);
            //        }
            //        return "success";
            //    }
            //    else
            //    {
            //        return "error";
            //    }
            //}


            string LianXiFangShiPanDuan = Request.Form["LianXiFangShiPanDuan"];
            string ShouJiHaoMa = Request.Form["ShouJiHaoMa"];
            string XiaoQu = Request.Form["XiaoQu"];

            //获取客户信息
            List<t_KeHuBiao> yanzhengkehu = it_KeHuBiaoService.GetList().Where(x => x.ShouJiHaoMa == ShouJiHaoMa && x.YiXiangXinXi == XiaoQu).ToList();
            //销售数据
            List<t_XiaoShouBiao> Xiaoshoubiao = new List<t_XiaoShouBiao>();
            int panduan = -1;
            //循环所有客户
            for (int j = 0; j < yanzhengkehu.Count(); j++)
            {
                panduan = 1;

                if (yanzhengkehu.Count > 0)
                {
                    Xiaoshoubiao = it_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == yanzhengkehu[j].Id).ToList();
                }
                else
                {
                    panduan = -1;
                }
                //判断客户是否已经是已失效
                if (yanzhengkehu[j].ZhuangTai != "已失效")
                {
                    while (panduan >= 1)
                    {
                        switch (panduan)
                        {
                            case 1://查询报备是否失效
                                //用 销售id 去查 销售详细 报备信息 时间
                                string caozuoshijian = it_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouBiaoId == Xiaoshoubiao[Xiaoshoubiao.Count() - 1].Id && x.XiaoShouDongZuo == "报备").ToList()[0].CaoZuoShiJian;
                                //判断系统时间是否大于报备的十五天之后
                                if (DateTime.Now < Convert.ToDateTime(caozuoshijian).AddDays(15))
                                {
                                    panduan = -2;
                                }
                                else
                                {
                                    //用 销售id 去查 销售详细 判断 报备最开始的操作时间 和 十五天之后的时间里 是否有 记录
                                    List<t_XiaoShouXiangXiBiao> Xiaoshouxiangxibiao = it_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouBiaoId == Convert.ToInt32(Xiaoshoubiao[Xiaoshoubiao.Count() - 1].Id) && Convert.ToDateTime(x.CaoZuoShiJian) > Convert.ToDateTime(caozuoshijian) && Convert.ToDateTime(x.CaoZuoShiJian) <= Convert.ToDateTime(caozuoshijian).AddDays(15)).ToList();
                                    if (Xiaoshouxiangxibiao.Count() > 0)
                                    {
                                        panduan = 2;
                                    }
                                    else
                                    {
                                        panduan = 3;
                                    }
                                }
                                break;
                            case 2://报备没有失效   判断跟进是否失效

                                //用 销售id 去查 销售详细 跟进信息id最大的一条
                                int xiaoshouxiangxiidgenjin = it_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouBiaoId == Xiaoshoubiao[Xiaoshoubiao.Count() - 1].Id && x.XiaoShouDongZuo != "报备").ToList().Max(x => x.Id);
                                //用 销售详细id  去查  销售详细的操作时间
                                string caozuoshijiangenjin = it_XiaoShouXiangXiBiaoService.GetList().Where(x => x.Id == Convert.ToInt32(xiaoshouxiangxiidgenjin)).ToList()[0].CaoZuoShiJian;
                                //判断系统时间是大于跟进三十天之后
                                if (DateTime.Now < Convert.ToDateTime(caozuoshijiangenjin).AddDays(30))
                                {
                                    panduan = -3;
                                }
                                else
                                {
                                    panduan = 3;
                                }
                                break;
                            case 3://已失效修改客户信息
                                var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                                optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                                using (var _context = new MyContext(optionsBuilder.Options))
                                {
                                    var KeHu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == yanzhengkehu[j].Id);
                                    KeHu.ZhuangTai = "已失效";
                                    if (_context.SaveChanges() > 0)
                                    {
                                        panduan = 4;
                                    }
                                    else
                                    {
                                        panduan = -4;
                                    }
                                }
                                break;
                            default:
                                panduan = -1;
                                break;
                        }
                    }
                }
                else
                {
                    panduan = -1;
                }
                if (panduan != -1)
                {
                    return panduan.ToString();
                }
            }
            if (panduan == -1)
            {
                //添加客户信息
                if (LianXiFangShiPanDuan != "")
                {
                    int shujiu = Convert.ToInt32(LianXiFangShiPanDuan);
                    it_KeHuZiYuanChiService.Delete(shujiu);
                }
                //获取当前登录人基本信息
                t_RenYuanBiao renyuan = it_RenYuanBiaoService.GetById(Convert.ToInt32(HttpContext.Session.GetString("UserId")));
                //获取所有职业顾问岗位
                List<t_GangWeiBiao> listgw = it_GangWeiBiaoService.GetList().Where(x => x.GangWeiMingCheng == "置业顾问").ToList();
                bool zhiye = false;
                try
                {
                    for (int i = 0; i < listgw.Count; i++)
                    {
                        if (listgw[i].Id == renyuan.GangWeiId)
                        {
                            zhiye = true;
                            break;
                        }

                    }
                }
                catch (Exception)
                {
                    return "error";
                    //throw;
                }
                string ZhuangTai = "已推荐";
                if (zhiye == true)
                {
                    ZhuangTai = "已到访";
                }
                t_KeHuBiao t_KeHuBiao = new t_KeHuBiao
                {
                    KeHuXingMing = Request.Form["XingMing"].ToString(),
                    ShouJiHaoMa = ShouJiHaoMa,
                    YiXiangDengJi = Request.Form["YiXiangDengJi"].ToString(),
                    LianMingKeHu = Request.Form["LianMingKeHu"].ToString(),
                    NianLing = Request.Form["NianLing"].ToString(),
                    XingBie = Request.Form["XingBie"].ToString(),
                    ZhengJianLeiXing = Request.Form["ZhengJianLeiXing"].ToString(),
                    ZhengJianHaoMa = Request.Form["ZhengJianHaoMa"].ToString(),
                    RenZhiMeiTi = Request.Form["RenZhiMeiTi"].ToString(),
                    KeHuBeiZhu = Request.Form["KeHuBeiZhu"].ToString(),
                    GeRenBiaoQian = Request.Form["GeRenBiaoQian"].ToString(),
                    ZhiYeShenFen = Request.Form["ZhiYeShenFen"].ToString(),
                    JiaTingJieGou = Request.Form["JiaTingJieGou"].ToString(),
                    JiaoYuChengDu = Request.Form["JiaoYuChengDu"].ToString(),
                    HunYinZhuangTai = Request.Form["HunYinZhuangTai"].ToString(),
                    ShouRuQingKuang = Request.Form["ShouRuQingKuang"].ToString(),
                    GongZuoQuYu = Request.Form["GongZuoQuYu"].ToString(),
                    GOngZuoDiZhi = Request.Form["GOngZuoDiZhi"].ToString(),
                    JuZhuQuYu = Request.Form["JuZhuQuYu"].ToString(),
                    JuZhuDiZhi = Request.Form["JuZhuDiZhi"].ToString(),
                    YiXiangXinXi = XiaoQu,
                    YiXiangYongTu = Request.Form["YiXiangYongTu"].ToString(),
                    YiXiangYeTai = Request.Form["YiXiangYeTai"].ToString(),
                    YiXiangHuXing = Request.Form["YiXiangHuXing"].ToString(),
                    YiXiangLouCeng = Request.Form["YiXiangLouCeng"].ToString(),
                    YiXiangMianJi = Request.Form["YiXiangMianJi"].ToString(),
                    YiXiangTaoShu = Request.Form["YiXiangTaoShu"].ToString(),
                    YiXiangDanJia = Request.Form["YiXiangDanJia"].ToString(),
                    YiXiangZongJia = Request.Form["YiXiangZongJia"].ToString(),
                    GuanZhuNeiRong = Request.Form["GuanZhuNeiRong"].ToString(),
                    ZhuangTai = ZhuangTai,
                    RenYuanId = Convert.ToInt32(HttpContext.Session.GetString("UserId")),
                    //RenYuanId = 2,
                    BeiZhu1 = System.DateTime.Now.ToString()
                };
                int KeHuId = it_KeHuBiaoService.Add(t_KeHuBiao).Id;
                if (KeHuId > 0)
                {

                    t_XiaoShouBiao xiaoshoubiao = new t_XiaoShouBiao();
                    xiaoshoubiao.KeHuId = KeHuId;
                    xiaoshoubiao.ShengChengShiJian = System.DateTime.Now.ToString();
                    //xiaoshoubiao.YeWuYuanId = 2;
                    if (zhiye == true)
                    {
                        xiaoshoubiao.ZhiYeGuWenId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
                    }
                    else
                    {
                        xiaoshoubiao.YeWuYuanId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
                    }

                    int XiaoShouBiaoId = it_XiaoShouBiaoService.Add(xiaoshoubiao).Id;

                    t_XiaoShouXiangXiBiao xiaoshouxiangxi = new t_XiaoShouXiangXiBiao();
                    xiaoshouxiangxi.XiaoShouDongZuo = "报备";
                    xiaoshouxiangxi.CaoZuoShiJian = System.DateTime.Now.ToString();
                    //xiaoshouxiangxi.CaoZuoRen = 2;
                    xiaoshouxiangxi.CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
                    xiaoshouxiangxi.XiaoShouBiaoId = XiaoShouBiaoId;
                    xiaoshouxiangxi.MiaoShu = "新增-报备";
                    xiaoshouxiangxi.ZhuangTai = "";
                    it_XiaoShouXiangXiBiaoService.Add(xiaoshouxiangxi);


                    if (ZhuangTai == "已到访")
                    {
                        t_XiaoShouXiangXiBiao xiaoshouxiangxi1 = new t_XiaoShouXiangXiBiao();
                        xiaoshouxiangxi1.XiaoShouDongZuo = "确认到访";
                        xiaoshouxiangxi1.CaoZuoShiJian = System.DateTime.Now.ToString();
                        //xiaoshouxiangxi.CaoZuoRen = 2;
                        xiaoshouxiangxi1.CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
                        xiaoshouxiangxi1.XiaoShouBiaoId = XiaoShouBiaoId;
                        xiaoshouxiangxi1.MiaoShu = "新增-确认到访";
                        xiaoshouxiangxi1.ZhuangTai = "";
                        it_XiaoShouXiangXiBiaoService.Add(xiaoshouxiangxi1);
                    }
                    return "5";
                }
                else
                {
                    return "-5";
                }
            }
            else
            {
                return "-4";
            }
        }

        [HttpPost]
        public string UpdateKeHu()
        {
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var KeHu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["Id"]));
                KeHu.KeHuXingMing = Request.Form["XingMing"].ToString();
                KeHu.ShouJiHaoMa = Request.Form["ShouJiHaoMa"].ToString();
                KeHu.YiXiangDengJi = Request.Form["YiXiangDengJi"].ToString();
                KeHu.LianMingKeHu = Request.Form["LianMingKeHu"].ToString();
                KeHu.NianLing = Request.Form["NianLing"].ToString();
                KeHu.XingBie = Request.Form["XingBie"].ToString();
                KeHu.ZhengJianLeiXing = Request.Form["ZhengJianLeiXing"].ToString();
                KeHu.ZhengJianHaoMa = Request.Form["ZhengJianHaoMa"].ToString();
                KeHu.RenZhiMeiTi = Request.Form["RenZhiMeiTi"].ToString();
                KeHu.KeHuBeiZhu = Request.Form["KeHuBeiZhu"].ToString();
                KeHu.GeRenBiaoQian = Request.Form["GeRenBiaoQian"].ToString();
                KeHu.ZhiYeShenFen = Request.Form["ZhiYeShenFen"].ToString();
                KeHu.JiaTingJieGou = Request.Form["JiaTingJieGou"].ToString();
                KeHu.JiaoYuChengDu = Request.Form["JiaoYuChengDu"].ToString();
                KeHu.HunYinZhuangTai = Request.Form["HunYinZhuangTai"].ToString();
                KeHu.ShouRuQingKuang = Request.Form["ShouRuQingKuang"].ToString();
                KeHu.GongZuoQuYu = Request.Form["GongZuoQuYu"].ToString();
                KeHu.GOngZuoDiZhi = Request.Form["GOngZuoDiZhi"].ToString();
                KeHu.JuZhuQuYu = Request.Form["JuZhuQuYu"].ToString();
                KeHu.JuZhuDiZhi = Request.Form["JuZhuDiZhi"].ToString();
                KeHu.YiXiangYongTu = Request.Form["YiXiangYongTu"].ToString();
                KeHu.YiXiangYeTai = Request.Form["YiXiangYeTai"].ToString();
                KeHu.YiXiangHuXing = Request.Form["YiXiangHuXing"].ToString();
                KeHu.YiXiangLouCeng = Request.Form["YiXiangLouCeng"].ToString();
                KeHu.YiXiangMianJi = Request.Form["YiXiangMianJi"].ToString();
                KeHu.YiXiangTaoShu = Request.Form["YiXiangTaoShu"].ToString();
                KeHu.YiXiangDanJia = Request.Form["YiXiangDanJia"].ToString();
                KeHu.YiXiangZongJia = Request.Form["YiXiangZongJia"].ToString();
                KeHu.GuanZhuNeiRong = Request.Form["GuanZhuNeiRong"].ToString();
                KeHu.RenYuanId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
                panduan = _context.SaveChanges() > 0;
            }

            t_KeHuBiao t_KeHuBiao = new t_KeHuBiao
            {
                Id = Convert.ToInt32(Request.Form["Id"]),

            };
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "error";
            }
        }

        //[HttpPost]
        //public string InitializationDataTable()
        //{
        //    t_KeHuBiao t_KeHuBiao = new t_KeHuBiao();
        //    string TiaoJian = Request.Form["TiaoJian"];
        //    t_KeHuBiao.KeHuXingMing = TiaoJian;
        //    t_KeHuBiao.ShouJiHaoMa = TiaoJian;
        //    t_KeHuBiao.ZhengJianHaoMa = TiaoJian;
        //    t_KeHuBiao.YiXiangDengJi = TiaoJian;
        //    t_KeHuBiao.XingBie = TiaoJian;
        //    t_KeHuBiao.LianMingKeHu = TiaoJian;
        //    return it_KeHuBiaoService.GetListWithFenYe(t_KeHuBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        //}

        [HttpPost]
        public string BaoBeiSubMit()
        {
            string UserId = HttpContext.Session.GetString("UserId");
            //string UserId = "2";
            int KeHuId = Convert.ToInt32(Request.Form["KeHuId"]);
            int YeWuYuanId = Convert.ToInt32(UserId);
            int XiaoShouBiaoId = 0;
            string XiaoShouDongZuo = Request.Form["CaoZuoLeiXing"]; ;
            string MiaoShu = Request.Form["BeiZhu"];


            List<t_XiaoShouBiao> listXiaoShouBiao = new List<t_XiaoShouBiao>();
            listXiaoShouBiao = it_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == KeHuId).ToList();

            if (listXiaoShouBiao.Count == 0)
            {
                t_XiaoShouBiao t_XiaoShouBiao = new t_XiaoShouBiao();
                t_XiaoShouBiao.KeHuId = KeHuId;
                t_XiaoShouBiao.ShengChengShiJian = System.DateTime.Now.ToString();
                t_XiaoShouBiao.YeWuYuanId = YeWuYuanId;
                XiaoShouBiaoId = it_XiaoShouBiaoService.Add(t_XiaoShouBiao).Id;

            }
            else
            {
                XiaoShouBiaoId = listXiaoShouBiao[0].Id;
            }
            if (XiaoShouBiaoId == 0)
            {
                return "error";
            }
            t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao = new t_XiaoShouXiangXiBiao();
            t_XiaoShouXiangXiBiao.XiaoShouDongZuo = XiaoShouDongZuo;
            t_XiaoShouXiangXiBiao.CaoZuoShiJian = System.DateTime.Now.ToString();
            t_XiaoShouXiangXiBiao.CaoZuoRen = YeWuYuanId;
            t_XiaoShouXiangXiBiao.XiaoShouBiaoId = XiaoShouBiaoId;
            t_XiaoShouXiangXiBiao.MiaoShu = MiaoShu;
            t_XiaoShouXiangXiBiao.ZhuangTai = "";
            int XiaoShouXiangXiBiaoId = it_XiaoShouXiangXiBiaoService.Add(t_XiaoShouXiangXiBiao).Id;

            if (XiaoShouXiangXiBiaoId > 0)
            {

                if (XiaoShouDongZuo == "报备")
                {
                    List<t_XiaoXiTiXingBiao> listxx = it_XiaoXiTiXingBiaoService.GetList().Where(x => x.KeHuId == KeHuId & x.TiXingLeiXing == "报备").ToList();
                    var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                    optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var XiaoXiTiXing = _context.t_XiaoXiTiXingBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(listxx[0].Id));
                        XiaoXiTiXing.TiXingShiJian = Convert.ToDateTime(XiaoXiTiXing.TiXingShiJian).AddHours(72).ToString();
                        _context.SaveChanges();
                    }
                    //t_XiaoXiTiXingBiao xiaoxitixing = it_XiaoXiTiXingBiaoService.GetById(Convert.ToInt32(listxx[0].Id));
                    //xiaoxitixing.TiXingShiJian = Convert.ToDateTime(xiaoxitixing.TiXingShiJian).AddHours(72).ToString();
                    //it_XiaoXiTiXingBiaoService.UpdateTiXing(xiaoxitixing);
                }
                else
                {
                    List<t_XiaoXiTiXingBiao> listxx = it_XiaoXiTiXingBiaoService.GetList().Where(x => x.KeHuId == KeHuId & x.TiXingLeiXing == "跟进").ToList();
                    var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                    optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var XiaoXiTiXing = _context.t_XiaoXiTiXingBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(listxx[0].Id));
                        XiaoXiTiXing.TiXingShiJian = Convert.ToDateTime(XiaoXiTiXing.TiXingShiJian).AddHours(72).ToString();
                        _context.SaveChanges();
                    }

                    //t_XiaoXiTiXingBiao xiaoxitixing = it_XiaoXiTiXingBiaoService.GetById(Convert.ToInt32(listxx[0].Id));
                    //xiaoxitixing.TiXingShiJian = Convert.ToDateTime(xiaoxitixing.TiXingShiJian).AddHours(48).ToString();
                    //it_XiaoXiTiXingBiaoService.UpdateTiXing(xiaoxitixing);
                }
                return "success";
            }
            else
            {
                return "error";
            }
        }

        [HttpPost]
        public string BindZhiYeGuWen()
        {
            string AnChangId = Request.Form["AnChangId"];

            string sql = "select * from t_RenYuanBiao where Id in ( select distinct ZhiYeGuWenId from t_TuanDuiRenYuanGuanLianBiao where TuanDuiId in (select distinct TuanDuiId from t_TuanDuiAnChangGuanLianBiao where AnChangId=" + AnChangId + " ))";

            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var context = new MyContext(optionsBuilder.Options))
            {
                var list2 = context.Database.SqlQuery<t_RenYuanBiao>(sql).ToList();
                string json = JsonConvert.SerializeObject(list2, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
                return json;
            }
        }

        [HttpPost]
        public string InitializationDataTableByKeHuId()
        {
            return Helper.ListToJsonAddFenYe(it_XiaoShouXiangXiBiaoService.GetByKeHuId(Convert.ToInt32(Request.Form["KeHuId"])), Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
        }


        [HttpPost]
        public string ReturnKeHuJson()
        {
            return JsonConvert.SerializeObject(new List<t_KeHuBiao> { it_KeHuBiaoService.GetById(Convert.ToInt32(Request.Form["Id"])) }, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string huiju()
        {
            int id = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            int shu = Convert.ToInt32(Request.Form["shu"]);
            int renyuanid = id;
            List<t_KeHuBiao> shuju = it_KeHuBiaoService.huibangQiYelist(renyuanid);
            int panduanshu = 10;
            if (shu != 0)
            {
                panduanshu += shu;
            }
            if (shuju.Count < panduanshu)
            {
                panduanshu = shuju.Count();
            }

            string fanhuizhi = "";
            for (int i = 0; i < panduanshu; i++)
            {
                //fanhuizhi += "<tr><td scope='row' style='text-align:center; height: 100px;'><a onclick='xiangxi()'>" + shuju[i].KeHuXingMing + "</a></td><td style='height: 100px;'>" + shuju[i].ShouJiHaoMa + "</td></tr>";
                fanhuizhi += "<tr><td scope='row' style='text-align:center;'><u style='color: rgb(105, 205, 255)'><a onclick='xiangxi(" + shuju[i].Id + ")'>" + shuju[i].KeHuXingMing + "</a></u></td><td>" + shuju[i].ShouJiHaoMa + "</td></tr>";
            }
            //for (int i = 0; i < panduanshu; i++)
            //{
            //    fanhuizhi += "<tr><th scope='row'><a onclick='xiangxi()'>" + i + "</a></th><td>" + i + "</td></tr>";
            //}
            return fanhuizhi;
        }
        public string jiazaigengduo()
        {
            int shu = Convert.ToInt32(Request.Form["shu"]);
            int renyuanid = Convert.ToInt32(HttpContext.Session.GetString("UserId")); ;
            List<t_KeHuBiao> shuju = it_KeHuBiaoService.huibangQiYelist(renyuanid);
            string fanhuizhi = "<u style='color: rgb(105, 205, 255)'><a style='font-size: 10px; text-align:center;' onclick='Load()'>加载更多>></a></u>";
            int panduanshu = 10;
            if (shu != 0)
            {
                panduanshu += shu;
            }
            if (shuju.Count < panduanshu)
            {
                fanhuizhi = "数据已全部加载完毕";
            }
            return fanhuizhi;
        }
        [HttpPost]
        public string huibangkehu()
        {
            int KeHuId = Convert.ToInt32(Request.Form["KeHuId"]);
            //Thread.Sleep(2000);
            t_KeHuBiao t_KeHuBiao = it_KeHuBiaoService.GetById(KeHuId);
            string json = "";
            //if (t_KeHuBiao.RenYuanId != Convert.ToInt32(HttpContext.Session.GetString("loginid")))
            //{
            //    json += "[{\"KeHuXingMing\":\"" + "***" + "\",\"ShouJiHaoMa\":\"" + "1**********" + "\",\"NianLing\":\"" + t_KeHuBiao.NianLing + "\",\"YiXiangDengJi\":\"" + t_KeHuBiao.YiXiangDengJi + "\",\"LianMingKeHu\":\"" + t_KeHuBiao.LianMingKeHu + "\",\"KeHuBeiZhu\":\"" + t_KeHuBiao.KeHuBeiZhu + "\",\"ZhiYeShenFen\":\"" + t_KeHuBiao.ZhiYeShenFen + "\",\"JiaTingJieGou\":\"" + t_KeHuBiao.JiaTingJieGou + "\",\"YiXiangLouCeng\":\"" + t_KeHuBiao.YiXiangLouCeng + "\",\"YiXiangTaoShu\":\"" + t_KeHuBiao.YiXiangTaoShu + "\",\"YiXiangMianJi\":\"" + t_KeHuBiao.YiXiangMianJi + "\",\"YiXiangHuXing\":\"" + t_KeHuBiao.YiXiangHuXing + "\",\"YiXiangHuXing\":\"" + t_KeHuBiao.YiXiangHuXing + "\",\"YiXiangYongTu\":\"" + t_KeHuBiao.YiXiangYongTu + "\",\"GuanZhuNeiRong\":\"" + t_KeHuBiao.GuanZhuNeiRong + "\",\"YiXiangZongJia\":\"" + t_KeHuBiao.YiXiangZongJia + "\",\"YiXiangDanJia\":\"" + t_KeHuBiao.YiXiangDanJia + "\"}]";
            //}
            //else
            //{
            json += "[{\"KeHuXingMing\":\"" + t_KeHuBiao.KeHuXingMing + "\",\"ShouJiHaoMa\":\"" + t_KeHuBiao.ShouJiHaoMa + "\",\"NianLing\":\"" + t_KeHuBiao.NianLing + "\",\"YiXiangDengJi\":\"" + t_KeHuBiao.YiXiangDengJi + "\",\"LianMingKeHu\":\"" + t_KeHuBiao.LianMingKeHu + "\",\"KeHuBeiZhu\":\"" + t_KeHuBiao.KeHuBeiZhu + "\",\"ZhiYeShenFen\":\"" + t_KeHuBiao.ZhiYeShenFen + "\",\"JiaTingJieGou\":\"" + t_KeHuBiao.JiaTingJieGou + "\",\"YiXiangLouCeng\":\"" + t_KeHuBiao.YiXiangLouCeng + "\",\"YiXiangTaoShu\":\"" + t_KeHuBiao.YiXiangTaoShu + "\",\"YiXiangMianJi\":\"" + t_KeHuBiao.YiXiangMianJi + "\",\"YiXiangHuXing\":\"" + t_KeHuBiao.YiXiangHuXing + "\",\"YiXiangHuXing\":\"" + t_KeHuBiao.YiXiangHuXing + "\",\"YiXiangYongTu\":\"" + t_KeHuBiao.YiXiangYongTu + "\",\"GuanZhuNeiRong\":\"" + t_KeHuBiao.GuanZhuNeiRong + "\",\"YiXiangZongJia\":\"" + t_KeHuBiao.YiXiangZongJia + "\",\"XingBie\":\"" + t_KeHuBiao.XingBie + "\",\"YiXiangDanJia\":\"" + t_KeHuBiao.YiXiangDanJia + "\"}]";
            //}
            return json;
        }
        public string Genjinjiluxiangxi()
        {
            int id = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            int kehuid = Convert.ToInt32(Request.Form["kehuid"]);
            List<t_XiaoShouXiangXiBiao> xiaoshou = it_XiaoShouXiangXiBiaoService.GetXiaoShouXiangXi(id, kehuid);
            StringBuilder Json = new StringBuilder();
            string huibaoleixingzhanshi = "";
            for (int i = 0; i < xiaoshou.Count; i++)
            {
                if (xiaoshou[i].XiaoShouDongZuo == "报备")
                {
                    huibaoleixingzhanshi = "<div class='icon-40 icon-circle align-self-start bg-success text-white'>B</div>";
                }
                else if (xiaoshou[i].XiaoShouDongZuo == "周报")
                {
                    huibaoleixingzhanshi = "<div class='icon-40 icon-circle align-self-start bg-warning text-white'>Z</div>";
                }
                else if (xiaoshou[i].XiaoShouDongZuo == "月报")
                {
                    huibaoleixingzhanshi = "<div class='icon-40 icon-circle align-self-start bg-primary text-white'>Y</div>";
                }
                else
                {
                    huibaoleixingzhanshi = "<div class='icon-40 icon-circle align-self-start bg-primary text-white'>Y</div>";
                }
                Json.Append("<tr class='even'><td>" + huibaoleixingzhanshi + "<h5 class='d-inline-block'>" + xiaoshou[i].XiaoShouDongZuo + "</h5></td><td>" + xiaoshou[i].CaoZuoShiJian + "</td>");
            }
            return Json.ToString();
        }
        [HttpPost]
        public string BindTodayGenJinJiLu()
        {
            var list = it_XiaoShouXiangXiBiaoService.GetList().Where(x => Convert.ToDateTime(x.CaoZuoShiJian) > Convert.ToDateTime(DateTime.Now.AddDays(-(Convert.ToInt32(Request.Form["Day"]))).ToString("yyyy-MM-dd") + " 23:59:59") && x.XiaoShouDongZuo == "跟进" && x.CaoZuoRen == Convert.ToInt32(HttpContext.Session.GetString("loginid"))).ToList();
            for (int i = 0; i < list.Count; i++)
            {
                list[i].t_XiaoShouBiao.t_KeHuBiao = it_KeHuBiaoService.GetById(list[i].t_XiaoShouBiao.KeHuId);
            }
            return JsonConvert.SerializeObject(list, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }

        [HttpPost]
        public string ShenQingLaiFang()
        {
            string UserId = HttpContext.Session.GetString("UserId");
            //string UserId = "2";
            int KeHuId = Convert.ToInt32(Request.Form["KeHuId"]);
            int ZhiYeGuWenId = Convert.ToInt32(Request.Form["ZhiYeGuWenId"]);
            int YeWuYuanId = Convert.ToInt32(UserId);
            int XiaoShouBiaoId = 0;
            string XiaoShouDongZuo = "到访";
            string MiaoShu = Request.Form["BeiZhu"];


            List<t_XiaoShouBiao> listXiaoShouBiao = new List<t_XiaoShouBiao>();
            listXiaoShouBiao = it_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == KeHuId).ToList();

            if (listXiaoShouBiao.Count == 0)
            {
                t_XiaoShouBiao t_XiaoShouBiao = new t_XiaoShouBiao();
                t_XiaoShouBiao.KeHuId = KeHuId;
                t_XiaoShouBiao.ShengChengShiJian = System.DateTime.Now.ToString();
                t_XiaoShouBiao.YeWuYuanId = YeWuYuanId;
                t_XiaoShouBiao.ZhiYeGuWenId = ZhiYeGuWenId;

                XiaoShouBiaoId = it_XiaoShouBiaoService.Add(t_XiaoShouBiao).Id;

            }
            else
            {
                var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                using (var _context = new MyContext(optionsBuilder.Options))
                {
                    var XiaoShouBiao = _context.t_XiaoShouBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(listXiaoShouBiao[0].Id));
                    XiaoShouBiao.ZhiYeGuWenId = ZhiYeGuWenId;
                    _context.SaveChanges();
                }
                //t_XiaoShouBiao xiaoshoubiao = it_XiaoShouBiaoService.GetById(Convert.ToInt32(listXiaoShouBiao[0].Id));
                //xiaoshoubiao.ZhiYeGuWenId = ZhiYeGuWenId;
                //it_XiaoShouBiaoService.Update(xiaoshoubiao);

                XiaoShouBiaoId = listXiaoShouBiao[0].Id;
            }
            if (XiaoShouBiaoId == 0)
            {
                return "error";
            }
            t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao = new t_XiaoShouXiangXiBiao();
            t_XiaoShouXiangXiBiao.XiaoShouDongZuo = XiaoShouDongZuo;
            t_XiaoShouXiangXiBiao.CaoZuoShiJian = System.DateTime.Now.ToString();
            t_XiaoShouXiangXiBiao.CaoZuoRen = YeWuYuanId;
            t_XiaoShouXiangXiBiao.XiaoShouBiaoId = XiaoShouBiaoId;
            t_XiaoShouXiangXiBiao.QueRenDaoFangRen = ZhiYeGuWenId;
            t_XiaoShouXiangXiBiao.MiaoShu = MiaoShu;
            t_XiaoShouXiangXiBiao.ZhuangTai = "未确认";
            int XiaoShouXiangXiBiaoId = it_XiaoShouXiangXiBiaoService.Add(t_XiaoShouXiangXiBiao).Id;

            if (XiaoShouXiangXiBiaoId > 0)
            {
                List<t_XiaoXiTiXingBiao> listxx = it_XiaoXiTiXingBiaoService.GetList().Where(x => x.KeHuId == KeHuId & x.TiXingLeiXing == "跟进").ToList();
                var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                using (var _context = new MyContext(optionsBuilder.Options))
                {
                    var XiaoXiTiXing = _context.t_XiaoXiTiXingBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(listxx[0].Id));
                    XiaoXiTiXing.TiXingShiJian = Convert.ToDateTime(XiaoXiTiXing.TiXingShiJian).AddHours(72).ToString();
                    _context.SaveChanges();
                }


                //t_XiaoXiTiXingBiao xiaoxitixing = it_XiaoXiTiXingBiaoService.GetById(Convert.ToInt32(listxx[0].Id));
                //xiaoxitixing.TiXingShiJian = Convert.ToDateTime(xiaoxitixing.TiXingShiJian).AddHours(48).ToString();
                //it_XiaoXiTiXingBiaoService.UpdateTiXing(xiaoxitixing);

                t_RenYuanBiao zhiyeguwen = it_RenYuanBiaoService.GetById(ZhiYeGuWenId);
                XiaoXiTiXing.SendMessage(zhiyeguwen.BeiZhu1, "有新客户到访!", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww500458ad942a211f&redirect_uri=http://web.xyufu.cn/PhoneKeHu/QueRenDaoFang?KeHuId=" + KeHuId + "&response_type=code&scope=snsapi_privateinfo&agentid=1000002&state=STATE#wechat_redirect");
                return "success";
            }
            else
            {
                return "error";
            }
        }
        [HttpPost]
        public string QueRenDaoFang1()
        {
            var updatexiaoshou = it_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == Convert.ToInt32(Request.Form["Id"])).ToList()[0].Id;
            t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao = new t_XiaoShouXiangXiBiao
            {
                XiaoShouDongZuo = "到访",
                CaoZuoShiJian = DateTime.Now.ToString(),
                XiaoShouBiaoId = it_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == Convert.ToInt32(Request.Form["Id"])).ToList()[0].Id,
                CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("loginid"))
            };
            it_XiaoShouXiangXiBiaoService.Add(t_XiaoShouXiangXiBiao);
            //var update = it_KeHuBiaoService.GetById(Convert.ToInt32(Request.Form["Id"]));
            //update.ZhuangTai = "确认到访";

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var KeHu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["Id"]));
                KeHu.ZhuangTai = "确认到访";
                panduan = _context.SaveChanges() > 0;
            }

            if (panduan)
            {
                return "true";
            }
            else
            {
                return "";
            }
        }

        [HttpPost]
        public string BindMyCustomer()
        {
            string LeiXing = Request.Form["LeiXing"];
            int RenYuanId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            //int RenYuanId = 2;
            List<t_KeHuBiao> listkehu = new List<t_KeHuBiao>();
            string Pai = Request.Form["Pai"];
            if (Pai == "正序")
            {
                listkehu = it_KeHuBiaoService.GetList().Where(x => x.RenYuanId == RenYuanId & x.ZhuangTai == LeiXing).OrderBy(x => Convert.ToDateTime(x.BeiZhu1)).ToList();
            }
            else if (Pai == "倒序")
            {
                listkehu = it_KeHuBiaoService.GetList().Where(x => x.RenYuanId == RenYuanId & x.ZhuangTai == LeiXing).OrderByDescending(x => Convert.ToDateTime(x.BeiZhu1)).ToList();
            }
            if (Request.Form["KeHuXingMing"] != "")
            {
                listkehu = listkehu.Where(x => x.KeHuXingMing == Request.Form["KeHuXingMing"]).OrderBy(x => x.BeiZhu1).ToList();
            }
            if (Request.Form["DianHuaHaoMa"] != "")
            {
                listkehu = listkehu.Where(x => x.ShouJiHaoMa == Request.Form["DianHuaHaoMa"]).OrderBy(x => x.BeiZhu1).ToList();
            }
            if (Request.Form["KaiShiShiJian"] != "" && Request.Form["JieShuShiJian"] != "")
            {
                listkehu = listkehu.Where(x => Convert.ToDateTime(x.BeiZhu1) > Convert.ToDateTime(Request.Form["KaiShiShiJian"]) && Convert.ToDateTime(x.BeiZhu1) < Convert.ToDateTime(Request.Form["JieShuShiJian"])).OrderBy(x => x.BeiZhu1).ToList();
            }
            else if (Request.Form["KaiShiShiJian"] == "" && Request.Form["JieShuShiJian"] != "")
            {
                listkehu = listkehu.Where(x => Convert.ToDateTime(x.BeiZhu1) < Convert.ToDateTime(Request.Form["JieShuShiJian"])).OrderBy(x => x.BeiZhu1).ToList();
            }
            else if (Request.Form["KaiShiShiJian"] != "" && Request.Form["JieShuShiJian"] == "")
            {
                listkehu = listkehu.Where(x => Convert.ToDateTime(x.BeiZhu1) > Convert.ToDateTime(Request.Form["KaiShiShiJian"])).OrderBy(x => x.BeiZhu1).ToList();
            }
            for (int i = 0; i < listkehu.Count(); i++)
            {
                listkehu[i].t_RenYuanBiao = null;
                listkehu[i].t_XiaoShouBiao = null;
                listkehu[i].t_XiaoXiTiXingBiaos = null;
            }
            string json = JsonConvert.SerializeObject(listkehu, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
            return json;
        }

        [HttpPost]
        public string BindMyCustomerTypeCount()
        {
            string UserId = HttpContext.Session.GetString("UserId");
            //string UserId = "57";
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));

            List<t_KeHuBiao> kehu = new List<t_KeHuBiao>();

            using (var context = new MyContext(optionsBuilder.Options))
            {
                kehu = context.t_KeHuBiao.ToList().Where(x => x.RenYuanId == Convert.ToInt32(UserId)).ToList();
            }
            if (Request.Form["KeHuXingMing"] != "")
            {
                kehu = kehu.Where(x => x.KeHuXingMing == Request.Form["KeHuXingMing"]).OrderBy(x => x.BeiZhu1).ToList();
            }
            if (Request.Form["DianHuaHaoMa"] != "")
            {
                kehu = kehu.Where(x => x.ShouJiHaoMa == Request.Form["DianHuaHaoMa"]).OrderBy(x => x.BeiZhu1).ToList();
            }
            if (Request.Form["KaiShiShiJian"] != "" && Request.Form["JieShuShiJian"] != "")
            {
                kehu = kehu.Where(x => Convert.ToDateTime(x.BeiZhu1) > Convert.ToDateTime(Request.Form["KaiShiShiJian"]) && Convert.ToDateTime(x.BeiZhu1) < Convert.ToDateTime(Request.Form["JieShuShiJian"])).OrderBy(x => x.BeiZhu1).ToList();
            }
            else if (Request.Form["KaiShiShiJian"] == "" && Request.Form["JieShuShiJian"] != "")
            {
                kehu = kehu.Where(x => Convert.ToDateTime(x.BeiZhu1) < Convert.ToDateTime(Request.Form["JieShuShiJian"])).OrderBy(x => x.BeiZhu1).ToList();
            }
            else if (Request.Form["KaiShiShiJian"] != "" && Request.Form["JieShuShiJian"] == "")
            {
                kehu = kehu.Where(x => Convert.ToDateTime(x.BeiZhu1) > Convert.ToDateTime(Request.Form["KaiShiShiJian"])).OrderBy(x => x.BeiZhu1).ToList();
            }
            int yirenchou = kehu.Where(x => x.ZhuangTai == "已认筹").ToList().Count;
            int yituijian = kehu.Where(x => x.ZhuangTai == "已推荐").ToList().Count;
            int yidaofang = kehu.Where(x => x.ZhuangTai == "已到访").ToList().Count;
            int yirengou = kehu.Where(x => x.ZhuangTai == "已认购").ToList().Count;
            int yiqianyue = kehu.Where(x => x.ZhuangTai == "已签约").ToList().Count;
            int yituifang = kehu.Where(x => x.ZhuangTai == "已退房").ToList().Count;
            int yishixiao = kehu.Where(x => x.ZhuangTai == "已失效").ToList().Count;
            string json = "[{\"YiRenChou\":\"" + yirenchou + "\",\"YiTuiJian\":\"" + yituijian + "\",\"YiDaoFang\":\"" + yidaofang + "\",\"YiRenGou\":\"" + yirengou + "\",\"YiQianYue\":\"" + yiqianyue + "\",\"YiTuiFang\":\"" + yituifang + "\",\"YiShiXiao\":\"" + yishixiao + "\"}]";

            return json;
        }


        #region 绑定MyCustomerDetail相关方法
        [HttpPost]
        public string BindKeHuJiBenXinXi()
        {
            int KeHuId = Convert.ToInt32(Request.Form["KeHuId"]);
            List<t_KeHuBiao> listkehu = it_KeHuBiaoService.GetList().Where(x => x.Id == KeHuId).ToList();
            string json = JsonConvert.SerializeObject(listkehu, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
            //"[{\"KeHuXingMing\":\""+kehu.KeHuXingMing+"\",\"ShouJiHaoMa\":\""+kehu.ShouJiHaoMa+"\",\"TuiJianShiJian\":\""+kehu.BeiZhu1+"\",\"ZhuangTai\":\""+kehu.ZhuangTai+"\"}]";
            return json;
        }
        [HttpPost]
        public string BindXiaoShouJiLu()
        {
            int KeHuId = Convert.ToInt32(Request.Form["KeHuId"]);
            List<t_XiaoShouBiao> listxiaoshou = listxiaoshou = it_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == KeHuId).ToList();

            //it_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == KeHuId).ToList();
            List<t_XiaoShouXiangXiBiao> listxiaoshouxiangxi = new List<t_XiaoShouXiangXiBiao>();
            if (listxiaoshou.Count > 0)
            {
                listxiaoshouxiangxi = it_XiaoShouXiangXiBiaoService.GetList();
                listxiaoshouxiangxi = listxiaoshouxiangxi.Where(x => x.XiaoShouBiaoId == listxiaoshou[0].Id).OrderBy(x => Convert.ToDateTime(x.CaoZuoShiJian)).ToList();
                //string json = JsonConvert.SerializeObject(listxiaoshouxiangxi, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
                if (listxiaoshouxiangxi.Count > 0)
                {
                    string json = "[";
                    for (int i = 0; i < listxiaoshouxiangxi.Count; i++)
                    {
                        t_RenYuanBiao renyuan = it_RenYuanBiaoService.GetById(Convert.ToInt32(listxiaoshouxiangxi[i].CaoZuoRen));

                        if (listxiaoshouxiangxi[i].XiaoShouDongZuo == "到访")
                        {
                            t_RenYuanBiao renyuanz = it_RenYuanBiaoService.GetById(Convert.ToInt32(listxiaoshouxiangxi[i].QueRenDaoFangRen));
                            json += "{\"XiaoShouDongZuo\":\"" + listxiaoshouxiangxi[i].XiaoShouDongZuo + "\",\"CaoZuoShiJian\":\"" + listxiaoshouxiangxi[i].CaoZuoShiJian + "\",\"MiaoShu\":\"" + listxiaoshouxiangxi[i].MiaoShu + "\",\"CaoZuoRen\":\"" + renyuan.XingMing + "\",\"CaoZuoRenDianHua\":\"" + renyuan.LianXiFangShi + "\",\"ZhiYeGuWen\":\"" + renyuanz.XingMing + "\",\"ZhiYeGuWenDianHua\":\"" + renyuanz.LianXiFangShi + "\"}";
                        }
                        else if (listxiaoshouxiangxi[i].XiaoShouDongZuo == "确认到访")
                        {
                            json += "{\"XiaoShouDongZuo\":\"" + listxiaoshouxiangxi[i].XiaoShouDongZuo + "\",\"CaoZuoShiJian\":\"" + listxiaoshouxiangxi[i].CaoZuoShiJian + "\",\"MiaoShu\":\"" + listxiaoshouxiangxi[i].MiaoShu + "\",\"CaoZuoRen\":\"" + renyuan.XingMing + "\",\"CaoZuoRenDianHua\":\"" + renyuan.LianXiFangShi + "\",\"QueRenZhuangTai\":\"" + listxiaoshouxiangxi[i].ZhuangTai + "\"}";
                        }
                        else
                        {
                            json += "{\"XiaoShouDongZuo\":\"" + listxiaoshouxiangxi[i].XiaoShouDongZuo + "\",\"CaoZuoShiJian\":\"" + listxiaoshouxiangxi[i].CaoZuoShiJian + "\",\"MiaoShu\":\"" + listxiaoshouxiangxi[i].MiaoShu + "\",\"CaoZuoRen\":\"" + renyuan.XingMing + "\",\"CaoZuoRenDianHua\":\"" + renyuan.LianXiFangShi + "\",\"QueRenZhuangTai\":\"" + listxiaoshouxiangxi[i].ZhuangTai + "\"}";
                        }

                        json += ",";

                    }
                    json = json.Substring(0, json.Length - 1);
                    json += "]";

                    return json;
                }
                else
                {
                    return "";
                }

            }
            else
            {
                return "";
            }

        }
        #endregion


        [HttpPost]
        public string QueRenDaoFangSubmit()
        {
            int KeHuId = Convert.ToInt32(Request.Form["KeHuId"]);
            string CaoZuoLeiXing = Request.Form["CaoZuoLeiXing"];
            int RenYuanId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            List<t_XiaoShouBiao> listxiaoshou = listxiaoshou = it_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == KeHuId).ToList();
            if (listxiaoshou.Count > 0)
            {
                string zhaungtai = "";
                if (CaoZuoLeiXing == "querendaofang")
                {
                    zhaungtai = "确认到访";

                    var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                    optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var KeHu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == KeHuId);
                        KeHu.ZhuangTai = "已到访";
                        _context.SaveChanges();
                    }


                    //t_KeHuBiao kehu = it_KeHuBiaoService.GetById(KeHuId);
                    //kehu.ZhuangTai = "已到访";
                    //it_KeHuBiaoService.Update(kehu);
                }
                if (CaoZuoLeiXing == "weidaofang")
                {
                    zhaungtai = "确认到访";
                }
                t_XiaoShouXiangXiBiao xiaoshouxiangxi = new t_XiaoShouXiangXiBiao();
                xiaoshouxiangxi.CaoZuoRen = RenYuanId;
                xiaoshouxiangxi.CaoZuoShiJian = System.DateTime.Now.ToString();
                xiaoshouxiangxi.MiaoShu = "确认到访";
                xiaoshouxiangxi.XiaoShouDongZuo = "确认到访";
                xiaoshouxiangxi.ZhuangTai = zhaungtai;
                xiaoshouxiangxi.XiaoShouBiaoId = listxiaoshou[0].Id;
                it_XiaoShouXiangXiBiaoService.Add(xiaoshouxiangxi);


                return "success";
            }
            else
            {
                return "error";
            }
        }


        [HttpPost]
        public string PanDuanShiFouQueRenGuo()
        {
            int KeHuId = Convert.ToInt32(Request.Form["KeHuId"]);
            List<t_XiaoShouBiao> listxiaoshou = listxiaoshou = it_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == KeHuId).ToList();
            if (listxiaoshou.Count > 0)
            {
                List<t_XiaoShouXiangXiBiao> xiaoshouxiangxi = it_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouBiaoId == listxiaoshou[0].Id).OrderByDescending(c => c.CaoZuoShiJian).ToList();

                int a = 0;
                for (int i = 0; i < xiaoshouxiangxi.Count; i++)
                {
                    if (xiaoshouxiangxi[i].XiaoShouDongZuo == "到访")
                    {
                        a = 2;
                        break;
                    }
                    if (xiaoshouxiangxi[i].XiaoShouDongZuo == "确认到访")
                    {
                        a = 1;
                        break;
                    }
                }

                if (a == 1)
                {
                    return "back";
                }
                else
                {
                    return "goon";
                }
            }
            else
            {
                return "error";
            }

        }


        [HttpPost]
        public string XiuGaiKeHu()
        {
            try
            {
                int KeHuId = Convert.ToInt32(Request.Form["KeHuId"]);
                string XingMing = Request.Form["XingMing"];
                string DianHua = Request.Form["DianHua"];
                string BeiZhu = Request.Form["BeiZhu"];
                string zt = Request.Form["ZhuangTai"];

                var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                using (var _context = new MyContext(optionsBuilder.Options))
                {
                    var kehu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == KeHuId);
                    if (zt == "失效")
                    {
                        kehu.BeiZhu2 = kehu.ZhuangTai;
                        kehu.ZhuangTai = "已失效";
                    }
                    else
                    {
                        kehu.ZhuangTai = kehu.BeiZhu2;
                    }

                    kehu.KeHuXingMing = XingMing;
                    kehu.ShouJiHaoMa = DianHua;
                    kehu.KeHuBeiZhu = BeiZhu;
                    _context.SaveChanges();


                    if (kehu.ZhuangTai == "已失效")
                    {
                        List<t_XiaoXiTiXingBiao> listxiaoxibb = it_XiaoXiTiXingBiaoService.GetList().Where(x => x.KeHuId == KeHuId && x.TiXingLeiXing == "报备").ToList();
                        using (var _contextbb = new MyContext(optionsBuilder.Options))
                        {
                            var xiaoxibb = _contextbb.t_XiaoXiTiXingBiao.FirstOrDefault(x => x.Id == listxiaoxibb[0].Id);
                            xiaoxibb.ShengYuTiXingCiShu = 0;
                            _contextbb.SaveChanges();
                        }
                        List<t_XiaoXiTiXingBiao> listxiaoxigj = it_XiaoXiTiXingBiaoService.GetList().Where(x => x.KeHuId == KeHuId && x.TiXingLeiXing == "跟进").ToList();

                        using (var _contextgj = new MyContext(optionsBuilder.Options))
                        {
                            var xiaoxigj = _contextgj.t_XiaoXiTiXingBiao.FirstOrDefault(x => x.Id == listxiaoxigj[0].Id);
                            xiaoxigj.ShengYuTiXingCiShu = 0;
                            _contextgj.SaveChanges();
                        }
                    }
                    else
                    {
                        List<t_XiaoXiTiXingBiao> listxiaoxibb = it_XiaoXiTiXingBiaoService.GetList().Where(x => x.KeHuId == KeHuId && x.TiXingLeiXing == "报备").ToList();
                        using (var _contextbb = new MyContext(optionsBuilder.Options))
                        {
                            var xiaoxibb = _contextbb.t_XiaoXiTiXingBiao.FirstOrDefault(x => x.Id == listxiaoxibb[0].Id);
                            xiaoxibb.ShengYuTiXingCiShu = 0;
                            xiaoxibb.TiXingShiJian = System.DateTime.Now.AddDays(3).ToString();
                            _contextbb.SaveChanges();
                        }

                        List<t_XiaoXiTiXingBiao> listxiaoxigj = it_XiaoXiTiXingBiaoService.GetList().Where(x => x.KeHuId == KeHuId && x.TiXingLeiXing == "跟进").ToList();
                        using (var _contextgj = new MyContext(optionsBuilder.Options))
                        {
                            var xiaoxigj = _contextgj.t_XiaoXiTiXingBiao.FirstOrDefault(x => x.Id == listxiaoxigj[0].Id);
                            xiaoxigj.ShengYuTiXingCiShu = 0;
                            xiaoxigj.TiXingShiJian = System.DateTime.Now.AddDays(3).ToString();
                            _contextgj.SaveChanges();
                        }
                    }
                }

                return "success";
            }
            catch (Exception)
            {
                return "error";
            }
        }


        [HttpPost]
        public string BindAnChang()
        {
            var list2 = it_AnChangBiaoService.GetList().Where(x => x.ZhuangTai == "启用").ToList();
            string json = JsonConvert.SerializeObject(list2, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
            return json;
        }

        #region 小区下拉框
        public string fanyuanliebiao()
        {
            return JsonConvert.SerializeObject(it_FangYuanBiaoService.GetList(), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        #endregion
    }
}

